home *** CD-ROM | disk | FTP | other *** search
- /*
- ManyWind TransSkel demonstration
-
- This application allows up to twenty windows to be created at once,
- with the New item under the File menu. The name of each window
- appears under the Windows menu (which is not created until at least
- one window exists). Selecting the window name from the Windows menu
- brings the window to the front. For every window created, Skel is
- told to create a new handler. If the window's close box is clicked,
- the handler removes the window name from the Windows menu, disposes
- of the window, and removes itself from the window handler list. If
- the window was the last window, the Windows menu handler removes
- itself from the menu handler list.
-
- When the first window is created, a Color menu also appears. This
- allows the color of the content region of the frontmost window to
- be changed. It goes away when the last window is closed.
-
- To quit, select Quit from the File menu or type command-Q.
-
- ManyWind demonstrates dynamic window and menu creation and disposal.
- It also shows how handler procedures may be shared among handlers
- for different windows.
-
- The project should include this file, TransSkel.c (or a project
- built from TransSkel.c), and MacTraps.
-
- 28 June 1986 Paul DuBois
- */
-
- # include <WindowMgr.h>
- # include <MenuMgr.h>
-
- # define nil 0L
- # define maxWind 20 /* maximum number of windows existing at once */
-
-
- enum /* menu numbers */
- {
- aMenuNum = 1, /* Apple menu */
- fMenuNum, /* File menu */
- wMenuNum, /* Windows menu */
- cMenuNum /* Color menu */
- };
-
-
- enum /* File menu item numbers */
- {
- new = 1,
- /* --- */
- quit = 3
- };
-
- enum /* Color menu items numbers */
- {
- cWhite = 1,
- cLtGray,
- cGray,
- cDkGray,
- cBlack
- };
-
-
- MenuHandle fileMenu;
- MenuHandle windowMenu;
- MenuHandle colorMenu;
-
- int windCount = 0; /* number of currently existing windows */
- long windNum = 0; /* id of last window created */
-
- int DoWindowMenu(), DoColorMenu(), DoMClobber();
-
-
- DoWUpdate ()
- {
- GrafPtr thePort;
-
-
- GetPort (&thePort);
- EraseRect (&thePort->portRect); /* repaint w/background pattern */
- }
-
-
- /*
- Mouse was clicked in close box. Remove the window handler (which
- causes the window to be disposed of), and delete the window title
- from the Windows menu. If the window was the last one, delete the
- Windows and Color menus entirely.
-
- Skel makes sure the port is pointing to the appropriate window, so
- this procedure can determine which window had its close box clicked,
- without being told explicitly.
- */
-
- DoWClose ()
- {
- GrafPtr thePort;
- MenuHandle m;
- int i, mItems;
- Str255 iTitle, wTitle;
-
- GetPort (&thePort); /* grafport of window to be closed */
- GetWTitle ((WindowPtr) thePort, wTitle);
- SkelRmveWind ((WindowPtr) thePort);
- if (--windCount == 0)
- {
- SkelRmveMenu (windowMenu); /* last window - clobber menus */
- SkelRmveMenu (colorMenu);
- }
- else
- {
- /* just take out of menu */
- m = NewMenu (wMenuNum, "\pWindows");
- for (i = 1, mItems = CountMItems (windowMenu); i <= mItems; ++i)
- {
- GetItem (windowMenu, i, iTitle);
- if (!EqualString (iTitle, wTitle, false, true))
- AppendMenu (m, iTitle);
- };
- SkelRmveMenu (windowMenu); /* remove old Windows menu */
- windowMenu = m; /* and install new one */
- SkelMenu (windowMenu, DoWindowMenu, DoMClobber);
- }
- EnableItem (fileMenu, new); /* can always create at least one more now */
- }
-
-
- /*
- Dispose of window. Skel makes sure the port is pointing to the
- appropriate window, so this procedure can determine which window
- is to be disposed, of without being told explicitly.
- */
-
- DoWClobber ()
- {
- GrafPtr thePort;
-
- GetPort (&thePort); /* grafport of window to dispose of */
- DisposeWindow ((WindowPtr) thePort);
- }
-
-
- DoMClobber (theMenu)
- MenuHandle theMenu;
- {
- DisposeMenu (theMenu);
- }
-
-
- /*
- Make new window. Locate at (100, 100) if no other windows, else
- offset slightly from front window. The window title is the next
- window number (1, 2, 3, ...). If this is the first window, create
- the Windows and Color menus. Add the window title as the last item
- of the Windows menu.
-
- If the maximum window count has been reached, disable New in the
- File menu.
- */
-
-
- MakeWindow ()
- {
- WindowPtr w;
- Rect r, r2;
- Str255 s;
-
- SetRect (&r, 0, 0, 200, 150);
- if ((w = FrontWindow ()) == nil)
- OffsetRect (&r, 100, 100);
- else
- {
- r2 = w->portBits.bounds;
- OffsetRect (&r, 20 - r2.left, 20 - r2.top);
- if (r.left > 480 || r.top > 300) /* keep on screen */
- OffsetRect (&r, 40 - r.left, 40 - r.top);
- }
- NumToString (++windNum, s);
- w = NewWindow (nil, &r, s, true, documentProc, -1L, true, 0L);
- SkelWindow (w,
- nil, /* mouseclicks */
- nil, /* key clicks */
- DoWUpdate, /* updates */
- nil, /* activate/deactivate events */
- DoWClose, /* close window, remove from menu */
- DoWClobber, /* dispose of window */
- nil, /* idle proc */
- false); /* irrelevant, since no idle proc */
-
- if (windCount++ == 0) /* if first window, create new menus */
- {
- colorMenu = NewMenu (cMenuNum, "\pColor");
- AppendMenu (colorMenu, "\pWhite;Light Gray;Gray;Dark Gray;Black");
- SkelMenu (colorMenu, DoColorMenu, DoMClobber);
- windowMenu = NewMenu (wMenuNum, "\pWindows");
- SkelMenu (windowMenu, DoWindowMenu, DoMClobber);
- }
- AppendMenu (windowMenu, s);
- if (windCount == maxWind)
- DisableItem (fileMenu, new);
-
- }
-
-
- DoFileMenu (item)
- int item;
- {
-
- switch (item)
- {
- case quit: SkelWhoa (); break; /* tell SkelMain to quit */
- case new: MakeWindow (); break; /* make a new window */
- }
- }
-
-
- DoWindowMenu (item)
- int item;
- {
- Str255 iTitle, wTitle;
- WindowPeek w;
-
- GetItem (windowMenu, item, iTitle); /* get window name */
- for (w = (WindowPeek) FrontWindow (); w != nil; w = w->nextWindow)
- {
- GetWTitle (w, wTitle);
- if (EqualString (iTitle, wTitle, false, true))
- {
- SelectWindow (w);
- break;
- }
- }
- }
-
-
- /*
- Change the background pattern of the frontmost window. Ignore
- if the front window is a DA window.
- */
-
- DoColorMenu (item)
- int item;
- {
- WindowPtr w;
-
- w = FrontWindow ();
- if (((WindowPeek) w)->windowKind < 0) return; /* front is DA window */
- switch (item)
- {
- case cWhite: BackPat (white); break;
- case cLtGray: BackPat (ltGray); break;
- case cGray: BackPat (gray); break;
- case cDkGray: BackPat (dkGray); break;
- case cBlack: BackPat (black); break;
- }
- EraseRect (&w->portRect);
- }
-
-
- main ()
- {
- SkelInit (); /* initialize */
- SkelApple (nil, nil); /* handle desk accessories */
- fileMenu = NewMenu (fMenuNum, "\pFile"); /* make File menu handler */
- AppendMenu (fileMenu, "\pNew/N;(-;Quit/Q");
- SkelGrowBounds (nil, 50, 10, 500, 300);
- SkelMenu (fileMenu, DoFileMenu, DoMClobber);
- SkelMain (); /* loop 'til Quit selected */
- SkelClobber (); /* clean up */
- }
-